package logic

import (
	"context"
	"errors"
	"shop-login-sys/model/mysql"

	"shop-login-sys/shoprpc/internal/svc"
	"shop-login-sys/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductAddLogic) ProductAdd(in *shoprpc.ProductAddRequest) (*shoprpc.ProductAddResponse, error) {
	// todo: add your logic here and delete this line
	var FindUserId mysql.User
	err := FindUserId.FindUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("用户登录成功才可以,进行商品添加")
	}
	product := &mysql.Product{
		//Model:  gorm.Model{},
		Name:   in.Name,
		Stock:  int(in.Stock),
		Price:  float64(in.Price),
		UserId: int(in.UserId),
	}
	err = product.ProductAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("商品添加成功")
	}
	return &shoprpc.ProductAddResponse{
		ProductId: int64(product.ID),
	}, nil
}
